package com.binguo.common.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author yuritian
 * @Title: UploadExcelService
 * @ProjectName cms
 * @Description: TODO
 * @date 2020/4/1015:24
 */
@Service
public class UploadExcelService {

    public String uploadExcel(Integer shopId,
                              String base64,
                              String suffix){
        JSONObject param = new JSONObject();
        param.put("shopId", shopId);
        param.put("bucketName", "excel");
        param.put("base64", base64);
        param.put("suffix", suffix);
        param.put("fileName",String.valueOf(System.currentTimeMillis()));

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        // 设置超时
        requestFactory.setConnectTimeout(1000);
        requestFactory.setReadTimeout(1000);

        RestTemplate restTemplate = new RestTemplate(requestFactory);

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.APPLICATION_JSON;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> entity = new HttpEntity<String>(param.toJSONString(), DEFAULT_JSON_HEADER);

        ResponseEntity<String> resp = restTemplate.postForEntity("https://tc.yunpengai.com/upload/api/minio/uploadFileByCustomer", entity, String.class);

        String createResult = resp.getBody();
        JSONObject json = JSONObject.parseObject(createResult);
        Integer code = json.getInteger("code");
        if (code.equals(200)) {
            return json.getString("data");
        }else {
            return null;
        }
    }
}
